package com.xinyue.game.center.account;

import com.xinyue.game.center.account.dao.entity.Account;
import com.xinyue.game.center.account.logic.AccountLoginService;
import com.xinyue.game.center.account.logic.AccountRegisterService;
import com.xinyue.game.center.account.params.AccountRegisterParam;
import com.xinyue.game.center.account.params.LoginParam;
import com.xinyue.game.center.account.response.VoLoginSuccess;
import com.xinyue.game.center.common.AbstractGameCenterServerController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author 王广帅
 * @Date 2021/4/29 1:05
 */
@RestController
@RequestMapping("account")
public class AccountController extends AbstractGameCenterServerController {

    @Autowired
    private AccountRegisterService accountRegisterService;
    @Autowired
    private AccountLoginService accountLoginService;

    @PostMapping("register")
    public Object registerAccount(@RequestBody AccountRegisterParam param) {
        Account account = new Account();
        account.setUsername(param.getUsername());
        account.setPassword(param.getPassword());
        accountRegisterService.accountRegister(account);
        VoLoginSuccess voLoginSuccess = loginSuccess(account);
        return success(voLoginSuccess);
    }

    private VoLoginSuccess loginSuccess(Account account) {
        VoLoginSuccess voLoginSuccess = new VoLoginSuccess();
        voLoginSuccess.setAccount(account);
        String token = "";
        voLoginSuccess.setToken(token);
        return voLoginSuccess;
    }

    @PostMapping("login")
    public Object loginAccount(@RequestBody LoginParam loginParam) {
        Account account = accountLoginService.login(loginParam);
        VoLoginSuccess loginSuccess = loginSuccess(account);
        return success(loginSuccess);
    }

}
